use registry::{Registry, NewCrate, NewCrateDependency};
use term::color::BLACK;
+use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET};
+
use core::source::Source;
use core::{Package, SourceId};
use core::dependency::Kind;
}
let (mut registry, _) = try!(registry(config, None, index));
- let crates = try!(registry.search(query, limit).map_err(|e| {
+ let (crates, total_crates) = try!(registry.search(query, limit).map_err(|e| {
human(format!("failed to retrieve search results from the registry: {}", e))
}));
try!(config.shell().say(line, BLACK));
}
+ let search_max_limit = 100;
+ if total_crates > limit as u32 && limit < search_max_limit {
+ try!(config.shell().say(
+ format!("... and {} crates more (use --limit N to see more)",
+ total_crates - limit as u32),
+ BLACK)
+ );
+ } else if total_crates > limit as u32 && limit >= search_max_limit {
+ try!(config.shell().say(
+ format!(
+ "... and {} crates more (go to http://crates.io/search?q={} to see more)",
+ total_crates - limit as u32,
+ percent_encode(query.as_bytes(), QUERY_ENCODE_SET)
+ ),
+ BLACK)
+ );
+ }
+
Ok(())
}
extern crate curl;
+extern crate url;
extern crate rustc_serialize;
use std::collections::HashMap;
use curl::http::handle::{Method, Request};
use rustc_serialize::json;
+use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET};
+
pub struct Registry {
host: String,
token: Option<String>,
#[derive(RustcDecodable)] struct ApiError { detail: String }
#[derive(RustcEncodable)] struct OwnersReq<'a> { users: &'a [&'a str] }
#[derive(RustcDecodable)] struct Users { users: Vec<User> }
-#[derive(RustcDecodable)] struct Crates { crates: Vec<Crate> }
+#[derive(RustcDecodable)] struct TotalCrates { total: u32 }
+#[derive(RustcDecodable)] struct Crates { crates: Vec<Crate>, meta: TotalCrates }
impl Registry {
pub fn new(host: String, token: Option<String>) -> Registry {
Ok(())
}
- pub fn search(&mut self, query: &str, limit: u8) -> Result<Vec<Crate>> {
- let body = try!(self.req(format!("/crates?q={}&per_page={}", query, limit), None, Get,
- Auth::Unauthorized));
+ pub fn search(&mut self, query: &str, limit: u8) -> Result<(Vec<Crate>, u32)> {
+ let formated_query = percent_encode(query.as_bytes(), QUERY_ENCODE_SET);
+ let body = try!(self.req(
+ format!("/crates?q={}&per_page={}", formated_query, limit),
+ None, Get, Auth::Unauthorized
+ ));
- Ok(json::decode::<Crates>(&body).unwrap().crates)
+ let crates = json::decode::<Crates>(&body).unwrap();
+ Ok((crates.crates, crates.meta.total))
}
pub fn yank(&mut self, krate: &str, version: &str) -> Result<()> {